### Environment constants 
TARGET_NAME := test

DATE := `date +%Y%m%d%H%M`
VERSION = module-factory-test-$(DATE)-01
APP_INSTALL =$(VERSION)-install.sh

CROSS_COMPILE ?= arm-linux-gnueabihf-
SOURCE_PATH = $(shell pwd)
TARGET_PATH = $(shell pwd)/app
RADIO_PATH = $(shell pwd)/radio
RELEASE_PATH = $(shell pwd)/release

LORA_RADIO_TARGETS =  $(RADIO_PATH)/sx1276/lib/libsx1276.a \
						$(RADIO_PATH)/sx1301/lib/libsx1301.a \
						$(RADIO_PATH)/md134x/lib/libmd134x.a


.PHONY: all clean
### general build targets
TEST_RADIO_TARGETS = $(TARGET_PATH)/half_duplex_test \
							$(TARGET_PATH)/full_duplex_test


all: dirs release
	@echo "---compile factory test ok--------------------------------"


dirs:
	mkdir -p $(RELEASE_PATH)
	
release: target
	-@tar -cf   release/$(VERSION).tar  app/target/*  app/cfg/* radio/lib_dll/*
	@touch $(APP_INSTALL)
	@echo '#! /bin/sh' >> $(APP_INSTALL)
	@echo "VERSION=${VERSION}" >> $(APP_INSTALL)
	@echo 'HOMEDIR=/home' >> $(APP_INSTALL)
	@echo 'CFGDIR=/cfg' >> $(APP_INSTALL)
	@echo 'if [ ! -d $${CFGDIR} ];then' >> $(APP_INSTALL)
	@echo '    mkdir $${CFGDIR}' >> $(APP_INSTALL)
	@echo 'fi' >> $(APP_INSTALL)
	@echo 'TEST=factory_test' >> $(APP_INSTALL)
	@echo 'if [ ! -d $${HOMEDIR}/$${TEST} ];then' >> $(APP_INSTALL)
	@echo '    mkdir $${HOMEDIR}/$${TEST}' >> $(APP_INSTALL)
	@echo 'fi' >> $(APP_INSTALL)
	@echo 'if [ ! -f $${VERSION}.tar ];then' >> $(APP_INSTALL)
	@echo '    echo $${VERSION}.tar not exist !' >> $(APP_INSTALL)
	@echo '    exit 1' >> $(APP_INSTALL)
	@echo 'fi' >> $(APP_INSTALL)
	@echo 'tar -xmvf $${VERSION}.tar --strip-components 0 -C $${HOMEDIR}/$${TEST}' >> ${APP_INSTALL}
	@echo 'cp -r $${HOMEDIR}/$${TEST}/radio/lib_dll/* /usr/local/lib/' >> ${APP_INSTALL}
	@echo 'export LD_LIBRARY_PATH=/usr/local/lib/:$$LD_LIBRARY_PATH' >> ${APP_INSTALL}
	@echo 'cp  $${HOMEDIR}/$${TEST}/app/target/halfduplex/factory_test $${HOMEDIR}/$${TEST}/$${TARGET_NAME}/test' >> ${APP_INSTALL}
	@echo 'cp  $${HOMEDIR}/$${TEST}/app/cfg/global_conf/global_conf_ali4701a2.json $${HOMEDIR}/$${TEST}/global_conf.json' >> ${APP_INSTALL}
	@echo 'cp  $${HOMEDIR}/$${TEST}/app/cfg/threhold_conf/config_n320_470_1a2.json $${HOMEDIR}/$${TEST}/config.json' >> ${APP_INSTALL}
	@echo 'cp  -r $${HOMEDIR}/$${TEST}/app/cfg/* $${CFGDIR}/' >> ${APP_INSTALL} 	
#@echo 'rm  -rf  $${HOMEDIR}/$${TEST}/.app' >> ${APP_INSTALL}	
#@echo 'cp  -r $${HOMEDIR}/$${TEST}/app $${HOMEDIR}/$${TEST}/.app' >> ${APP_INSTALL} 	
	@echo 'rm  -rf  $${HOMEDIR}/$${TEST}/app' >> ${APP_INSTALL}
	@echo 'rm  -rf  $${HOMEDIR}/$${TEST}/radio' >> ${APP_INSTALL}    
	@echo 'chmod +x  $${HOMEDIR}/$${TEST}/$${TARGET_NAME}' >> ${APP_INSTALL}
	@mv ${APP_INSTALL} ${RELEASE_PATH}/


target:
	@$(MAKE) all -C radio
	@$(MAKE) all -C app

clean:
	$(MAKE) clean -C radio
	$(MAKE) clean -C app
	@rm -rf $(RELEASE_PATH)/*
	@echo "---clean factory test ok----------------------------------"   
    
### EOF

